<?php

namespace app\admin\validate;

use think\Validate;

class ConfigGroup extends Validate
{
    protected $failException = true;
    /**
     * 验证规则
     */
    protected $rule = [
        'title'  => 'require|unique:config_group',
        'name'   => 'require|unique:config_group',
        'status' => 'checkStatus',
        'weigh'  => 'number'
    ];
    /**
     * 验证提示信息
     */
    protected $message = [

    ];
    /**
     * 字段描述
     */
    protected $field = [];

    /**
     * 验证场景
     */
    protected $scene = [
        'add'        => ['name', 'value', 'weigh', 'status'],
        'edit'       => ['name', 'value', 'weigh', 'status'],
        'editStatus' => ['status'],
    ];

    public function __construct()
    {
        $this->field   = [
            'name'  => '分组名称',
            'value' => '分组值',
            'weigh' => '权重'
        ];
        $this->message = array_merge($this->message, [
            'name.require'  => "分组名称不能为空",
            'value.require' => "分组值不能为空",
            'weigh.number'  => "权重只能为正整数类型",
        ]);
        parent::__construct();
    }

    /**
     * 角色状态状态自定义验证规则
     * @param $value // 需验证的值
     * @param $rule // 验证规则
     * @param array $data // 数据
     */
    protected function checkStatus($value, $rule, array $data = [])
    {
        $status = (string)$data['status'] ?? ''; // 验证码
        if ($status === '') {
            return __('No rows updated');
        } else if ($status !== '0' && $status !== '1') {
            return __('No rows updated');
        }
        return true;
    }
}